<template>
	<view class="u-wrap">
		
		<view class="u-search-box">
			<u-search :show-action="false" placeholder="请输入商品搜索" disabled @click="gosearch"></u-search>
		</view>
		<!-- 一级导航 -->
		<u-tabs :list="tabbar" :is-scroll="false" v-model="currents" @change="change" active-color="#fd5d33"></u-tabs>
		<view class="u-menu-wrap" v-if="erji.length>0">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<view v-for="(item,index) in erji" :key="index" class="u-tab-item"
					:class="[current==index ? 'u-tab-item-active' : '']" :data-current="index"
					@tap.stop="swichMenu(index,item)">
					<text class="u-line-1">{{item.title}}</text>
				</view>
			</scroll-view>
			<block>
				<scroll-view scroll-y class="right-box" @sscrolltolower="getCategoryIds(currentId,page ++)">
					<view class="page-view" v-if="subTabbar.length>0">
						<view class="class-item">
							<view class="item-container">
								<view class="thumb-box" v-for="(item1, index1) in subTabbar" :key="index1"
									@click="handlerDetails(item1)">
									<view class="item-menu-image">
										<image :src="item1.images_format[0]" mode=""></image>
									</view>
									<view class="item-menu-info">
										<view class="shopName">{{item1.name}}</view>
										<view class="shopPrice">
											<view class="priceBox">
												<span class="price">￥{{item1.min_price}}</span>
												<span class="cost_price">￥{{item1.cost_price}}</span>
											</view>
											<u-icon class="icon" size="40" name="plus-circle-fill" color="#6bc665"
												@click.stop.prevent="addCart(item1)"></u-icon>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="page_noData" v-else>
						<image src="/static/empty.png" mode=""></image>
					</view>
				</scroll-view>
			</block>
		</view>
	</view>
</template>

<script>
	// import classifyData from "@/common/classify.data.js";
	import {
		getCategorylist,
		categoryIds
	} from '@/api/product.js';
	import {
		shopGoodsCartAdd
	} from '@/api/product.js'
	import {
		toast
	} from '../../utils/common';
	export default {
		data() {
			return {
				keyword: '',
				tabbar: [],
				subTabbar: [],
				erji: [],
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				status: 'loadmore',
				page: 1,
				currentId: 0,
				currents: 0,
				falg: false,
				sortId:''
			}
		},
		watch: {
			currentId: {
				handler(val, old) {
					if (val != old) {
						this.subTabbar = []
					}
				},
				deep: true
			}
		},
		onReachBottom() {
			this.status = 'loading';
			this.page = ++this.page;
			// console.log(222);
			setTimeout(() => {
				this.getCategoryIds(this.currentId, this.page)
			}, 1000)
		},
		onLoad(res) {
			console.log(res);
			this.sortId = Number(res.id)
			this.getCategorylist()

		},
		methods: {
			getImg() {
				return Math.floor(Math.random() * 35);
			},
			gosearch() {
				uni.navigateTo({
					url: '/page/product-list/product-list'
				})
			},
			gethhh() {
				
				this.currents = this.sortId||0
				console.log("最后一次",this.currents);
				this.changes(this.currents)


			},
			change(index) {
				this.current = 0
				this.erji = this.tabbar[index].erji
				if (this.erji.length > 0) {
					this.getCategoryIds(this.tabbar[index].erji[0].id, this.page);
					this.currentId = this.tabbar[index].erji[0].id
				} else {
					this.subTabbar = []
				}


			},
			changes(index) {
				if (!this.falg) {
					console.log("我执行力",index);
					this.erji = this.tabbar[index].erji
					if (this.erji.length > 0) {
						this.getCategoryIds(this.tabbar[index].erji[0].id, this.page);
						this.currentId = this.tabbar[index].erji[0].id
					} else {
						this.subTabbar = []
					}
				}


			},

			getCategorylist() {

				let that = this;
				that.subTabbar = []
				this.tabbar = []
				this.erji = []
				getCategorylist().then(res => {

					that.tabbar = res.data.map((item) => {
						return {
							...item,
							name: item.title
						}
					});
					this.current = 0
					this.gethhh()
					console.log("eeee",that.tabbar);
					that.erji = that.tabbar[this.currents].erji
					that.getCategoryIds(that.tabbar[this.currents].erji[0].id, this.page);
					that.currentId = that.tabbar[this.currents].erji[0].id
				})
			},
			getCategoryIds(id, page) {
				this.falg = true
				let that = this;
				categoryIds(id, {
					page: page
				}).then(res => {
					// console.log('数据',res.list);
					if (res.list.length < 10) {
						this.status = 'nomore';
					}
					if (res.list.length != 0) {
						this.subTabbar = res.list					}
				})
			},
			// 加入购物车
			addCart(item) {
				let obj = {
					goods_id: item.id,
					goods_number: 1
				}
				shopGoodsCartAdd(obj).then(res => {
					// console.log(8888,res);
					toast('添加成功')
				})
			},
			handlerDetails(item) {
				uni.navigateTo({
					url: '/pages/goods-details/goods-details?id=' + item.id
				})
			},
			// 点击左边的栏目切换
			async swichMenu(index, item) {
				if (index == this.current) return;
				this.current = index;
				this.currentId = item.id;
				this.getCategoryIds(item.id);
				// 如果为0，意味着尚未初始化
				if (this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({
						size: true
					}, res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			}
		},
		
		onHide() {
			this.falg = false
		}


	}
</script>

<style lang="scss" scoped>
	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
		background-color: #f5f5f5;
	}

	.u-search-box {
		padding: 18rpx 30rpx;
		margin-bottom: 20upx;
		background-color: #fff;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	.u-search-inner {
		background-color: rgb(234, 234, 234);
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 16rpx;
	}

	.u-search-text {
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}

	.u-tab-view {
		width: 200rpx;
		height: 100%;
	}

	.u-tab-item {
		height: 110rpx;
		background: #f6f6f6;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
	}

	.u-tab-item-active {
		position: relative;
		color: #000;
		font-size: 30rpx;
		font-weight: 600;
		background: rgba(249, 198, 207, 0.5);
		border-left: 4px solid #fd5d33;
	}

	// .u-tab-item-active::before {
	// 	content: "";
	// 	position: absolute;
	// 	border-left: 4px solid #6bc665;
	// 	height: 32rpx;
	// 	left: 0;
	// 	top: 39rpx;
	// }

	.u-tab-view {
		height: 100%;
	}

	.right-box {
		background-color: #fff;
	}

	.page-view {
		padding: 16rpx;
	}

	.class-item {
		margin-bottom: 30rpx;
		background-color: #fff;
		padding: 16rpx;
		border-radius: 8rpx;
	}

	.item-title {
		font-size: 28rpx;
		color: $u-main-color;
		font-weight: bold;
		padding: 10upx 0 10upx;
	}

	.item-menu-name {
		font-weight: normal;
		font-size: 24rpx;
		color: $u-main-color;
	}

	.item-container {
		// display: flex;
		// flex-wrap: wrap;
	}

	.thumb-box {
		// width: 33.333333%;
		display: flex;
		// align-items: center;
		// justify-content: center;
		// flex-direction: column;
		margin-top: 20rpx;
	}

	.item-menu-image {
		width: 160rpx;
		height: 160rpx;
		border-radius: 8rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.item-menu-info {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		margin-left: 10upx;

		.shopPrice {
			// flex: 1;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}
		
		.shopName {
			font-size: 26upx;
		}
		
		.icon {
			float: right;
		}
		
		.price {
			font-size: 34upx;
			font-weight: 500;
			color: #fa453c;
		}
		.cost_price{
			text-decoration: line-through;
			font-size: 22rpx;
			margin-left: 10rpx;
			color: #555;
		}
	}

	.page_noData image {
		margin-top: 100rpx;
		width: 100%;
	}
</style>